package com.ihighest.app.infox.activity;

import java.util.Timer;
import java.util.TimerTask;

import org.json.JSONException;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.ihighest.app.common.api.Api;
import com.ihighest.app.common.api.ApiCallback;
import com.ihighest.app.common.api.ApiParam;
import com.ihighest.app.common.api.ApiResult;
import com.ihighest.app.common.kit.AppKit;
import com.ihighest.app.common.kit.SharedPreferencesKit;
import com.ihighest.app.common.kit.ToastKit;
import com.ihighest.app.infox.Const;
import com.ihighest.app.infox.R;
import com.ihighest.app.infox.myview.ActivityTitle;
import com.ihighest.app.infox.tools.RegexKit;

/**
 * 修改密码界面
 * 
 * @author lkx Created at 2015-10-3 下午8:48:42
 * @version 1.0
 */
public class ChangePasswordActivity extends BaseActivity implements OnClickListener {
	private ActivityTitle title;
	String phoneNumber;
	String oldpsd;
	Button getcodeButton;
	Button sureButton;
	int time = 0;
	String codeId;
	private EditText codeEditText, newpsdEditText1, newpsdEditText2;
	private Handler handler;
	private Timer timers;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.infox_activity_changepassword);
		title = (ActivityTitle) findViewById(R.id.infox_changePsdActivity_title);
		title.setTitle("修改密码");
		title.setViewVersible(title.titleBack, title.rightText);
		title.setViewGone(title.titleMsg, title.titleRefresh);
		title.setRightTextViewText("取消");
		timers = new Timer();
		title.rightText.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		
		inint();

	}

	private void inint() {
		phoneNumber = SharedPreferencesKit.getString(getApplicationContext(), Const.SP_KEY_LAST_LOGIN_KEY);
		oldpsd = SharedPreferencesKit.getString(getApplicationContext(), Const.SP_KEY_LAST_LOGIN_PWD);
		getcodeButton = (Button) findViewById(R.id.infox_findPsd_getCode);
		getcodeButton.setOnClickListener(this);
		sureButton = (Button) findViewById(R.id.button1);
		sureButton.setOnClickListener(this);
		codeEditText = (EditText) findViewById(R.id.infox_findPsd_textcode);
		newpsdEditText1 = (EditText) findViewById(R.id.infox_findPsd_inputNewPsd);
		newpsdEditText2 = (EditText) findViewById(R.id.editText1);

		handler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				super.handleMessage(msg);

				switch (msg.what) {
				case 0x123:
					time = time - 1;
					if (time <= 0) {

						getcodeButton.setText("获取验证码");
						getcodeButton.setClickable(true);
						getcodeButton.setBackgroundResource(R.drawable.infox_content_comment_bg);
					} else {

						getcodeButton.setText(time + "s后重新获取");
					}
					break;

				}

			}
		};

	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.infox_findPsd_getCode:

			Api.post("mobilecaptcha", ApiParam.create().addParam("phone", phoneNumber).addParam("type", "update"), new ApiCallback(
					ChangePasswordActivity.this) {

				@Override
				public void handleSuccess(ApiResult result) {
					try {
						codeId = result.getData().getString("captcha_id");
						time = result.getData().getInt("time");

						handler.sendEmptyMessage(0X123);
						getcodeButton.setClickable(false);

						getcodeButton.setBackgroundResource(R.drawable.infox_register_btn_gettextcode_bg);
						timers.schedule(new TimerTask() {

							@Override
							public void run() {

								handler.sendEmptyMessage(0x123);
							}

						}, 0, 1000);

					} catch (JSONException e) {
						e.printStackTrace();
					}

				}

				@Override
				public void handleFailure(Exception e, String msg) {

				}
			}, ChangePasswordActivity.this);

			break;

		case R.id.button1:
			sureButton.setClickable(false);

			if (codeEditText.getText().toString().equals("") || codeEditText.getText().toString() == null) {
				ToastKit.showShort(ChangePasswordActivity.this, "验证码不能为空");

			}

			else if (!RegexKit.isPassword(newpsdEditText1.getText().toString())) {
				ToastKit.showShort(ChangePasswordActivity.this, "密码为6到20位数字或字母");

			} else if (!newpsdEditText1.getText().toString().equals(newpsdEditText2.getText().toString())) {
				ToastKit.showShort(ChangePasswordActivity.this, "两次输入密码不一致");
			}

			else {

				Api.post(
						"me/pwd",
						ApiParam.create().addParam("newpass", AppKit.md5(newpsdEditText1.getText().toString()))
								.addParam("oldpass", AppKit.md5(oldpsd)).addParam("captcha", codeEditText.getText().toString())
								.addParam("captcha_id", codeId), new ApiCallback(ChangePasswordActivity.this) {

							@Override
							public void handleSuccess(ApiResult result) {
								if (result.getCode() == 100) {
									ToastKit.showShort(ChangePasswordActivity.this, "修改密码成功");
									sureButton.setClickable(true);
									timers.cancel();
									finish();
								}else{
									
									sureButton.setClickable(true);
									ToastKit.showShort(ChangePasswordActivity.this, "服务器忙，稍后重试");
								}
								

							}

							@Override
							public void handleFailure(Exception e, String msg) {
								sureButton.setClickable(true);
								ToastKit.showShort(ChangePasswordActivity.this, "服务器忙，稍后重试");
							}
						}, ChangePasswordActivity.this);
			}
			sureButton.setClickable(true);

			break;
		}

	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		timers.cancel();
	}

}
